package cn.org.donald.framework.util;

import java.lang.reflect.Method;

/**
 * Reflect util
 * @author : Donald
 * @since : 2021-01-09 20:08:03
 **/
public class ReflectUtil {

    /**
     * Search method from target class and its super class by regular until find the target method out.
     * @param clazz class
     * @param methodName method name
     * @return The target method
     */
    public static Method getMethodRegular(Class<?> clazz, String methodName){
        if (Object.class.equals(clazz)) {
            return null;
        } else {
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    return method;
                }
            }
            return getMethodRegular(clazz.getSuperclass(), methodName);
        }
    }
}
